package com.atguigu.yygh.hosp.controller.admin;

import com.atguigu.yygh.common.result.Result;
import com.atguigu.yygh.common.result.ResultCodeEnum;
import com.atguigu.yygh.common.utils.MD5;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.Hospital;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(tags = "医院设置接口的管理")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
//@CrossOrigin
public class AdminHospitalSetController {

    @Autowired
    private HospitalSetService hospitalSetService;

    @ApiOperation(value = "医院设置发送签名和密钥")
    @GetMapping("/singKey/{id}")
    public Object sendKey(@PathVariable(value = "id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        if (ObjectUtils.isEmpty(hospitalSet)) {
            return Result.fail("该记录不存在,可能被删除啦");
        }
        String signKey = hospitalSet.getSignKey();
        String hoscode = hospitalSet.getHoscode();
        //发送短信服务,后补
        return Result.ok();
    }

    @ApiOperation(value = "医院设置的锁定与解锁")
    @PutMapping("/lockHosp/{id}/{status}")
    public Object lockHosp(@PathVariable("id") @ApiParam(value = "医院设置锁定的id") Long id,
                           @PathVariable("status") @ApiParam(value = "医院的状态", example = "1") Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return Result.ok();

    }

    @ApiOperation(value = "批量删除医院设置列表")
    @DeleteMapping("/removeBatch")
    public Object removeBatch(@RequestBody
                              @ApiParam(value = "参数批量删除的医院设置的id")
                                      List<Long> ids) {
        if (ObjectUtils.isEmpty(ids)) {
            return Result.fail(ResultCodeEnum.PARAM_ERROR);
        }
        // 先查询有没有该医院的记录
        List<HospitalSet> hospitalSetList = hospitalSetService.listByIds(ids);
        if (ObjectUtils.isEmpty(hospitalSetList)) {
            return Result.fail("该记录不存在!");
        }
        //进行删除操作
        boolean res = hospitalSetService.removeBatchByIds(ids);
        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


    @ApiOperation(value = "医院设置的条件分页查询")
    // @GetMapping("/findPageHospitalSet/{current}/{limit}")
    @PostMapping("/findPageHosp/{current}/{limit}")
    public Object findPageHospital(
            @ApiParam(value = "查询页")
            @PathVariable("current") Long current,
            @PathVariable("limit")
            @ApiParam(value = "每页数据条数") Long limit,
            //HospitalSetQueryVo hsq
            @RequestBody(required = false) HospitalSetQueryVo hospitalSetQueryVo
    ) {
        //创建page对象，传递当前页，每页记录数
        Page<HospitalSet> page = new Page<>(current,limit);
        //构建条件
        QueryWrapper<HospitalSet> wrapper = new QueryWrapper<>();
        String hosname = hospitalSetQueryVo.getHosname();//医院名称
        String hoscode = hospitalSetQueryVo.getHoscode();//医院编号
        if(!StringUtils.isEmpty(hosname)) {
            wrapper.like("hosname",hospitalSetQueryVo.getHosname());
        }
        if(!StringUtils.isEmpty(hoscode)) {
            wrapper.eq("hoscode",hospitalSetQueryVo.getHoscode());
        }

        //调用方法实现分页查询
        IPage<HospitalSet> pageHospitalSet = hospitalSetService.page(page, wrapper);

        //返回结果
        return Result.ok(pageHospitalSet);
    }


    @ApiOperation(value = "查询所有医院设置列表")
    @GetMapping("/findAll")
    public Object findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return Result.ok(list);
    }

    @ApiOperation(value = "查询指定id医院设置列表")
    @GetMapping("/findById/{id}")
    public Result findById(
            @ApiParam(value = "医院设置列表的id", required = true, example = "2")
            @PathVariable("id") Long id) {
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        System.out.println("hospitalSet = " + hospitalSet);

        if (ObjectUtils.isEmpty(hospitalSet)) {
            return Result.fail("该记录不存在,可能被删除啦");
        }
        Integer deleted = hospitalSet.getIsDeleted();
        if (deleted == 1) {
            return Result.fail("该记录不存在,可能被删除啦");
        }
        return Result.ok(hospitalSet);
    }

    @ApiOperation(value = "指定id修改医院设置的对象")
    @PutMapping("/updateHospitalSet")
    public Result updateById(@RequestBody
                             @ApiParam(value = "医院设置的对象", required = true)
                                     HospitalSet hospitalSet) {
        boolean res = hospitalSetService.updateById(hospitalSet);
        return Result.ok();
    }

    @ApiOperation(value = "逻辑删除指定id医院设置列表")
    @DeleteMapping("/removeHospById/{id}")
    public Result removeHospById(@PathVariable("id")
                                 @ApiParam(value = "逻辑删除的id", example = "2") Long id) {
        boolean res = hospitalSetService.removeById(id);
        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }

    @ApiOperation(value = "保存医院设置的对象")
    @PostMapping("/saveHosp")
    public Object save(@RequestBody
                       @ApiParam(value = "参数保存医院设置的对象", required = true) HospitalSet hospitalSet) {

        if (!ObjectUtils.isEmpty(hospitalSet)) {
            hospitalSet.setStatus(1);
        }
        // 签名密钥,医院设置加密
        Random random = new Random();
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis() + "" + random.nextInt(1000)));
        // 调用save方法
        boolean res = hospitalSetService.save(hospitalSet);
        if (res) {
            return Result.ok();
        } else {
            return Result.fail();
        }
    }


}
